﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BBPA.BLL
{
    public partial class Orders
    {
        /// <summary>
        /// 获取某孕婴店当前订单。如果没有订单，就自动创建一个新的
        /// </summary>
        /// <param name="modelStore_">要获取当前订单的孕婴店</param>
        /// <returns></returns>
        public static Model.Orders GetModelOrders_Now(Model.Store modelStore_)
        {
            Model.Orders modelOrders = new Model.Orders();
            BLL.Orders bllOrders = new Orders();
            List<Model.Orders> listModelOrders_WaitMoney = bllOrders.GetModelList("StoreID=" + modelStore_.id + " and State<=" + (int)EnumHelper.OrdersState.WaitMoney);
            if (listModelOrders_WaitMoney.Count == 0)
            {
                modelOrders.StoreID = modelStore_.id;
                modelOrders.Number =DateTime.Now.ToString("yyyyMMddHHmm") + modelStore_.Number;
                modelOrders.State = (int)EnumHelper.OrdersState.NoGoods;
                int OrdersID_new = bllOrders.Add(modelOrders);
                modelOrders = bllOrders.GetModel(OrdersID_new);
            }
            else modelOrders = listModelOrders_WaitMoney[0];
            return modelOrders;
        }
        /// <summary>
        /// 订单预计的售价合计（也即：孕婴店进价）
        /// </summary>
        /// <param name="OrdersID"></param>
        /// <returns></returns>
        public static decimal Get_Orders_ExpectSellPriceSum(object OrdersID)
        {
            decimal returnVal = 0;
            BLL.Orders bll_order = new BLL.Orders();
            Model.Orders model_order = bll_order.GetModel(Convert.ToInt32(OrdersID));
            if (model_order.Number.ToString().StartsWith("tg"))
            {
               
                if (model_order.Gbid > 0)
                {
                    BBPA.BLL.Group_Good gg_bll = new BLL.Group_Good();
                    DataTable dt = gg_bll.GetList(" OrdersID=" + OrdersID).Tables[0];
                    foreach(DataRow dr in dt.Rows)
                    {
                        returnVal += (Convert.ToDecimal(dr["gggoodprice"]) * Convert.ToInt32(dr["ExpectAmount"]));
                    }
                }
            }
            else
            {
                BLL.V_Orders_Goods bllOrders_Goods = new V_Orders_Goods();
                foreach (Model.V_Orders_Goods model in bllOrders_Goods.GetModelList("OrdersID=" + OrdersID))
                {
                    returnVal += BLL.Goods.GetSellPriceSum(model.GoodsID, model.ExpectAmount);
                }
            }
            return returnVal;
        }
        /// <summary>
        /// 订单预计的联盟进价合计
        /// </summary>
        /// <param name="OrdersID"></param>
        /// <returns></returns>
        public static decimal Get_Orders_ExpectBuyPriceSum(object OrdersID)
        {
            decimal returnVal = 0;
            BLL.V_Orders_Goods bllOrders_Goods = new V_Orders_Goods();
            foreach (Model.V_Orders_Goods model in bllOrders_Goods.GetModelList("OrdersID=" + OrdersID))
            {
                returnVal += BLL.Goods.GetBuyPriceSum(model.GoodsID, model.ExpectAmount);
            }
            return returnVal;
        }
        /// <summary>
        /// 订单预计给供货商支付的采购款
        /// </summary>
        /// <param name="OrdersID"></param>
        /// <returns></returns>
        public static decimal Get_Orders_ExpectPay(object OrdersID)
        {
            decimal returnVal = 0;
            BLL.V_Orders_Goods bllOrders_Goods = new V_Orders_Goods();
            foreach (Model.V_Orders_Goods model in bllOrders_Goods.GetModelList("OrdersID=" + OrdersID+" and IsLatePay=0"))
            {
                returnVal += BLL.Goods.GetBuyPriceSum(model.GoodsID, model.ExpectAmount);
            }
            return returnVal;
        }
        /// <summary>
        /// 订单预计【在收货点】给供货商支付的收货款
        /// </summary>
        /// <param name="OrdersID"></param>
        /// <returns></returns>
        public static decimal Get_Orders_ExpectPay_Delivery(object OrdersID)
        {
            decimal returnVal = 0;
            BLL.V_Orders_Goods bllOrders_Goods = new V_Orders_Goods();
            foreach (Model.V_Orders_Goods model in bllOrders_Goods.GetModelList("OrdersID=" + OrdersID + " and IsLatePay=0 and IsDelivery=1"))
            {
                returnVal += BLL.Goods.GetBuyPriceSum(model.GoodsID, model.ExpectAmount);
            }
            return returnVal;
        }
        /// <summary>
        /// 订单预计【现场】给供货商支付的采购款
        /// </summary>
        /// <param name="OrdersID"></param>
        /// <returns></returns>
        public static decimal Get_Orders_ExpectPay_notDelivery(object OrdersID)
        {
            decimal returnVal = 0;
            BLL.V_Orders_Goods bllOrders_Goods = new V_Orders_Goods();
            foreach (Model.V_Orders_Goods model in bllOrders_Goods.GetModelList("OrdersID=" + OrdersID + " and IsLatePay=0 and IsDelivery=0"))
            {
                returnVal += BLL.Goods.GetBuyPriceSum(model.GoodsID, model.ExpectAmount);
            }
            return returnVal;
        }
        //收款时计算需要补的货款+100运费
        public static decimal bukuan(object MasterAccount_, object FrozenAccount_, object ExpectSellPriceSum_)
        {
            decimal returnVal = DataTypePlus.requestDecimal(ExpectSellPriceSum_) - (DataTypePlus.requestDecimal(MasterAccount_) - DataTypePlus.requestDecimal(FrozenAccount_)) + 100;
            if (returnVal < 0) return 0;
            else return returnVal;
        }
        public static string bukuanStr(object MasterAccount_, object FrozenAccount_, object ExpectSellPriceSum_)
        {
            decimal returnVal = bukuan( MasterAccount_,  FrozenAccount_,  ExpectSellPriceSum_);
            if (returnVal < 0) return "";
            else return StringPlus.showMoney(returnVal);
        }
    }
}
